package com.kaka.bus.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kaka.bus.R;
import com.kaka.bus.bean.ProductDetailBean;

import java.util.List;

public class InfoAdapter extends RecyclerView.Adapter {
    public static int TYPE_HEAD = 0;
    public static int TYPE_NORMAL = 1;


    private Activity mActivity;
    private List<ProductDetailBean> beans;
    private InFoOnclick onclick;

    public InfoAdapter(Activity mActivity, InFoOnclick inFoOnclick, List<ProductDetailBean> beans) {
        this.mActivity = mActivity;
        this.beans = beans;
        onclick = inFoOnclick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            View headerView = LayoutInflater.from(mActivity).inflate(R.layout.item_infoheader, null, false);
            return new MyHeadlerHolder(headerView);
        } else {
            View View = LayoutInflater.from(mActivity).inflate(R.layout.item_infopro, null, false);
            return new Holder(View);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHeadlerHolder) {

        }
        if (holder instanceof Holder) {
            Holder mHolder = (Holder) holder;
            mHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onclick.onItemInfoClick(position);
                }
            });
            Glide.with(mActivity).load(beans.get(position).getProductIcon()).into(mHolder.img_mainpro);
            mHolder.tv_mainprotitle.setText(beans.get(position).getProductName());
        }
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (beans.get(position).getProductType() == -1) {
            return TYPE_HEAD;
        } else {
            return TYPE_NORMAL;
        }
    }

    class MyHeadlerHolder extends RecyclerView.ViewHolder {

        public MyHeadlerHolder(View itemView) {
            super(itemView);
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView img_mainpro;
        TextView tv_mainprotitle;

        public Holder(View itemView) {
            super(itemView);
            img_mainpro = itemView.findViewById(R.id.img_mainpro);
            tv_mainprotitle = itemView.findViewById(R.id.tv_mainprotitle);
        }
    }

    public boolean isHeader(int position) {
        if (beans.get(position).getProductType() == -1) {
            return true;
        } else {
            return false;
        }
    }

    public interface InFoOnclick {
        void onItemInfoClick(int point);
    }
}
